package com.heima.search.service.impl;

import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.search.pojos.ApAssociateWords;
import com.heima.search.service.AssociateWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author jack
 * @data 2024 16:12
 */
@Service
public class AssociateWordServiceImpl implements AssociateWordService {
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 搜索提示词
     *
     * @param map
     * @return
     */
    public ResponseResult search(Map map) {
        if (map == null)
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);

        //1. 获取请求参数
        Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
        String searchWords = map.get("searchWords").toString();

        Query query = Query.query(Criteria.where("associateWords")
                .regex(searchWords, "i")
                ).with(Sort.by(Sort.Direction.DESC,"createdTime"))
                .with(PageRequest.of(1,10));

        List<ApAssociateWords> list = mongoTemplate.find(query, ApAssociateWords.class);

        return ResponseResult.okResult(list);
    }
}
